/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 *
 * Contributor(s):
 *
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
 */
package org.nbphpcouncil.modules.php.ci.ui.options;

import javax.swing.AbstractListModel;
import org.openide.util.NbBundle;

/**
 *
 * @author Junji Takakura
 */
final class CIOptionsPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private final CIOptionsPanelController controller;
    private StorablePanel[] categoryPanels;

    CIOptionsPanel(CIOptionsPanelController controller) {
        this.controller = controller;
        initComponents();

        categoryPanels = new StorablePanel[]{
            new CategoryPanelGeneral(),
            new CategoryArchiveFiles(CIEntry.Type.BASE),
            new CategoryArchiveFiles(CIEntry.Type.LANGUAGE_PACK),};

        categoriesList.setModel(new AbstractListModel() {

            private static final long serialVersionUID = 1L;
            String[] categories = {
                getMessage("LBL_CATEGORY_GENERAL"),
                getMessage("LBL_CATEGORY_BASE_FILES"),
                // NbBundle.getMessage(CIOptionsPanel.class, "LBL_CATEGORY_LANGUAGE_PACKS")
            };

            @Override
            public int getSize() {
                return categories.length;
            }

            @Override
            public Object getElementAt(int index) {
                return categories[index];
            }
        });

        categoriesList.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        categoriesPanel = new javax.swing.JPanel();
        categoriesList = new javax.swing.JList();
        categoryPanel = new javax.swing.JPanel();

        categoriesPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        categoriesPanel.setPreferredSize(new java.awt.Dimension(150, 285));

        categoriesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "General", "CodeIgniter Files", "Language Packs" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        categoriesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                categoriesListValueChanged(evt);
            }
        });

        javax.swing.GroupLayout categoriesPanelLayout = new javax.swing.GroupLayout(categoriesPanel);
        categoriesPanel.setLayout(categoriesPanelLayout);
        categoriesPanelLayout.setHorizontalGroup(
            categoriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(categoriesList, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
        );
        categoriesPanelLayout.setVerticalGroup(
            categoriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(categoriesList, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
        );

        categoryPanel.setLayout(new javax.swing.BoxLayout(categoryPanel, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(categoriesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(categoryPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(categoriesPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
            .addComponent(categoryPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void categoriesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_categoriesListValueChanged
        int index = categoriesList.getSelectedIndex();

        if (index >= 0 && index <= 2) {
            selectCategory(index);
        }
    }//GEN-LAST:event_categoriesListValueChanged

    void load() {
        for (StorablePanel panel : categoryPanels) {
            panel.load();
        }
    }

    void store() {
        for (StorablePanel panel : categoryPanels) {
            panel.store();
        }
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }

    private void selectCategory(int index) {
        if (categoryPanel.getComponentCount() > 0) {
            categoryPanel.removeAll();
        }

        categoryPanel.add(categoryPanels[index]);
        categoryPanel.revalidate();
        categoryPanel.repaint();
    }
    
    private String getMessage(String resourceName, Object... parameters) {
        return NbBundle.getMessage(CIOptionsPanel.class, resourceName, parameters);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList categoriesList;
    private javax.swing.JPanel categoriesPanel;
    private javax.swing.JPanel categoryPanel;
    // End of variables declaration//GEN-END:variables
}
